#!/usr/bin/env python
# coding: utf-8

# In[1]:


exec(open('init_path.py').read())
exec((P_Lib/'GasStation.py').read_text())
get_ipython().run_line_magic('matplotlib', 'inline')


# ### Price Point at Certain Time

# In[10]:


File = read_hdf(P_GS_Data_Raw / 'PH_Day.h5', 'GS')
File = File[(~File.Holiday) & File.Weekday].copy()
print(File.shape)
File = File[File.intYMD // 10000 >= 2016]
print(File.shape)
print(File.head(2))


# In[42]:


ls = []
for ymd in File.intYMD.tolist():
    if ymd%100==1: print(ymd, end=',')
    f = P_GS_Data_Raw / 'PH_Day' / (str(ymd)+'.h5')
    df = read_hdf(f, 'GS')
    ls_stid = df.StID.unique().tolist()
    ls_time = [Timestamp(str(ymd)+' '+str(h)+':00') for h in [9,12,17,19]]
    base = DataFrame(itertools.product(ls_stid, ls_time), columns = ['StID','Time'])
    df = df.append(base).sort_values(['StID','Time']).fillna(method='ffill')
    ls.append(df[df.Time.isin(ls_time)])


# In[43]:


df = concat(ls)


# In[44]:


for fuel in ['diesel','e5','e10']:
    df.loc[df[fuel]<0, fuel] = np.nan


# In[46]:


df.head(2)


# In[47]:


df.to_stata(P_GS_Data / 'Price' / 'price_at_9_12_17_19.dta', write_index=False)


# In[41]:


df


# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:




